import { Repository, UpdateResult } from "typeorm";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { UserResourceEntity } from "../entity/UserResourceEntity";

export class UserResourceDao extends DaoBase {

    public static Tb_Name: string = "t_resources";

    public static async getRepository() {
        let repository: Repository<UserResourceEntity> = await this.getRepositoryBase(UserResourceEntity, this.getGameDbName())
        return repository;
    }

    public static async update_user(entity: UserResourceEntity) {
        let repository: Repository<UserResourceEntity> = await this.getRepository()
        let result: UserResourceEntity
        if (!repository.hasId(entity)) {
            result = await repository.save(entity)
        } else {
            let updateResult:UpdateResult = await repository.update({ userid: entity.userid }, entity)
             // { generatedMaps: [], raw: [], affected: 1 }
             if (updateResult && updateResult.affected > 0) {
                result = entity
            }
        }
        return result
    }

 
    public static async getResource(userid: number) {
        let repository: Repository<UserResourceEntity> = await this.getRepository()
        let entity: UserResourceEntity = await repository.findOneBy({
            userid: userid
        })
        if (!entity) {
            entity = new UserResourceEntity()
            entity.userid = userid
            await repository.save(entity)
        }
        return entity
    }



}